import debounce from "lodash/debounce";

export default {
	inserted(el, binding) {
		const callback = debounce(binding.value, 50, { leading: true });
		const options = binding.options || { passive: true };

		window.addEventListener("resize", callback, options);
		el._onResize = {
			callback,
			options
		};

		if (!binding.modifiers || !binding.modifiers.quiet) {
			callback();
		}
	},
	unbind(el) {
		if (!el._onResize) return;

		const { callback, options } = el._onResize;
		window.removeEventListener("resize", callback, options);
		delete el._onResize;
	}
};
